class Observer {
  constructor(data) {
    this.walk(data)
  }
  walk(data) {
    if(!data || typeof data !== 'object'){
      return
    }
    Object.keys(data).forEach(key => {
      this.defineReactive(data,key,data[key])
    })
  }
  defineReactive(obj, key, val) {
    const self = this
    let dep = new Dep()
    // 如果值是对象
    this.walk(val)
    Object.defineProperty(obj,key,{
      enumerable:true,
      configurable:true,
      get() {
        Dep.target && dep.addSub(Dep.target)
        return val
      },
      set(v) {
        if(v === val){
          return
        }
        val = v
        self.walk(v)
        dep.notify()
      }
    })
  }
}